package com.rio.helper.json;

import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.rio.core.U;
import com.rio.helper.json.G;

public class GHelper {

	public static String CODE = "ErrorCode";
	public static String MESSAGE = "ErrorMessage";
	public static String CONTENT = "Content";

	public static <T> GObject<T> pasre2Obj(String data, Class<T> cls)
			throws JSONException {
		JSONObject obj = new JSONObject(data);
		GObject<T> result = new GObject<T>();
		result.code = obj.has(CODE) ? obj.getInt(CODE) : 0;
		result.message = obj.has(MESSAGE) ? obj.getString(MESSAGE) : null;
		String json = obj.has(CONTENT) ? obj.getString(CONTENT) : null;
		if (U.notNull(json)) {
			result.data = G.toObject(json, cls);
		}
		return result;
	}

	public static <T> GList<T> pasre2List(String data, Class<T> cls)
			throws JSONException {
		JSONObject obj = new JSONObject(data);
		GList<T> result = new GList<T>();
		result.code = obj.has(CODE) ? obj.getInt(CODE) : 0;
		result.message = obj.has(MESSAGE) ? obj.getString(MESSAGE) : null;
		String json = obj.has(CONTENT) ? obj.getString(CONTENT) : null;
		if (U.notNull(json)) {
			result.data = G.toList(json, cls);
		}
		return result;
	}

	public static <T> GList<T> pasre2List(JSONObject data, Class<T> cls)
			throws JSONException {
		GList<T> result = null;
		if (U.notNull(data)) {
			result = new GList<T>();
			result.code = data.has(CODE) ? data.getInt(CODE) : 0;
			result.message = data.has(MESSAGE) ? data.getString(MESSAGE) : null;
			String json = data.has(CONTENT) ? data.getString(CONTENT) : null;
			if (U.notNull(json)) {
				result.data = G.toList(json, cls);
			}
		}
		return result;
	}

	public static <T> GObject<T> pasre2Obj(JSONObject data, Class<T> cls)
			throws JSONException {
		GObject<T> result = null;
		if (U.notNull(data)) {
			result = new GObject<T>();
			result.code = data.has(CODE) ? data.getInt(CODE) : 0;
			result.message = data.has(MESSAGE) ? data.getString(MESSAGE) : null;
			String json = data.has(CONTENT) ? data.getString(CONTENT) : null;
			if (U.notNull(json)) {
				result.data = G.toObject(json, cls);
			}
		}
		return result;
	}

	public static <T> T toObject(String data, Class<T> cls)
			throws JSONException {
		JSONObject obj = new JSONObject(data);
		JSONObject json = obj.has(CONTENT) ? obj.getJSONObject(CONTENT) : null;
		if (U.notNull(json)) {
			return G.toObject(json.toString(), cls);
		}
		return null;
	}

	public static <T> T toObject(JSONObject data, Class<T> cls)
			throws JSONException {
		if (U.notNull(data)) {
			JSONObject json = data.has(CONTENT) ? data.getJSONObject(CONTENT)
					: null;
			if (U.notNull(json)) {
				return G.toObject(json.toString(), cls);
			}
		}
		return null;
	}

	public static <T> List<T> toList(String data, Class<T> cls)
			throws JSONException {
		JSONObject obj = new JSONObject(data);
		JSONArray json = obj.has(CONTENT) ? obj.getJSONArray(CONTENT) : null;
		if (U.notNull(json)) {
			return G.toList(json, cls);
		}
		return null;
	}

	public static String getString(String data, String key)
			throws JSONException {
		JSONObject json = getData(data);
		if (U.notNull(json)) {
			return json.has(key) ? json.getString(key) : null;
		}
		return null;
	}

	public static int getInt(String data, String key) throws JSONException {
		JSONObject json = getData(data);
		if (U.notNull(json)) {
			return json.has(key) ? json.getInt(key) : -1;
		}
		return -1;
	}

	public static JSONObject getData(String data) throws JSONException {
		JSONObject obj = new JSONObject(data);
		JSONObject json = obj.has(CONTENT) ? obj.getJSONObject(CONTENT) : null;
		return json;
	}

}
